package com.fancoder.kafka;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;

public class ProducerInterceptor implements org.apache.kafka.clients.producer.ProducerInterceptor<String, String> {
    private int partitions = 2;

    public ProducerRecord<String, String> onSend(ProducerRecord<String, String> producerRecord) {
        String key = producerRecord.key();
        int hashCode = key.hashCode();
        int partition = hashCode % partitions;
        return new ProducerRecord<String, String>(producerRecord.topic(), partition, producerRecord.timestamp(),
                producerRecord.key(), producerRecord.value(), producerRecord.headers());
    }

    public void onAcknowledgement(RecordMetadata recordMetadata, Exception e) {
    }

    public void close() {

    }

    public void configure(Map<String, ?> map) {
    }

    public void setPartitions(int partitions) {
        this.partitions = partitions;
    }
}
